﻿namespace EasyWeb
{
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;

    public static class EWResourceCultureHelper
    {
        private static readonly ReadOnlyCollection<string> resourceCultureNames = new ReadOnlyCollection<string>(new[]
        {
            "en",
            "zh-Hans",
        });

        public static string DefaultCultureName
        {
            get { return "en"; }
        }

        public static IReadOnlyList<string> CultureNames
        {
            get { return resourceCultureNames; }
        }

        public static string GetCultureName(string name)
        {
            string result;

            if (!TryGetCultureName(name, out result))
            {
                throw new InvalidOperationException(string.Format("The culture name '{0}' is not supported yet.", name));
            }

            return result;
        }

        public static bool TryGetCultureName(string name, out string resourceCultureName)
        {
            EWGuard.IsNotNullOrEmpty(name, "name");

            resourceCultureName = null;

            if (StringComparer.OrdinalIgnoreCase.Compare(name, "en") == 0 || name.StartsWith("en-", StringComparison.OrdinalIgnoreCase))
            {
                resourceCultureName = "en";
            }
            else if (name.StartsWith("zh-", StringComparison.OrdinalIgnoreCase))
            {
                if (StringComparer.OrdinalIgnoreCase.Compare(name, "zh-Hans") == 0 ||
                    StringComparer.OrdinalIgnoreCase.Compare(name, "zh-CN") == 0 ||
                    StringComparer.OrdinalIgnoreCase.Compare(name, "zh-CHS") == 0)
                {
                    resourceCultureName = "zh-Hans";
                }
            }

            return resourceCultureName != null;
        }

        public static bool IsSupportedCultureName(string name)
        {
            EWGuard.IsNotNullOrEmpty(name, "name");

            return StringComparer.OrdinalIgnoreCase.Compare(name, "en") == 0 ||
                   name.StartsWith("en-", StringComparison.OrdinalIgnoreCase) ||
                   StringComparer.OrdinalIgnoreCase.Compare(name, "zh-Hans") == 0 ||
                   StringComparer.OrdinalIgnoreCase.Compare(name, "zh-CN") == 0 ||
                   StringComparer.OrdinalIgnoreCase.Compare(name, "zh-CHS") == 0;
        }
    }
}
